package cn.com.jenson.web.file.controller;

import cn.com.jenson.common.BasicController;
import cn.com.jenson.common.ImportVerifierVo;
import cn.com.jenson.common.Result;
import cn.com.jenson.common.SelectVo;
import cn.com.jenson.dao.file.bo.FileBo;
import cn.com.jenson.dao.file.searchBo.FileSearchBo;
import cn.com.jenson.dao.file.vo.FileVo;
import cn.com.jenson.service.file.service.FileService;
import org.springframework.core.io.InputStreamResource;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/api/file")
public class FileController implements BasicController<FileVo,FileBo, FileSearchBo> {

    @Resource
    FileService fileService;

    @Override
    public Result<Page<FileVo>> pageAll(@RequestBody FileSearchBo fileSearchBo) {
        return Result.suc(fileService.pageAll(fileSearchBo));
    }

    @Override
    public Result<List<FileVo>> listAll(@RequestBody FileSearchBo fileSearchBo) {
        return Result.suc(fileService.listAll(fileSearchBo));
    }

    @Override
    public Result<FileVo> get(@RequestBody FileSearchBo fileSearchBo) throws InstantiationException, IllegalAccessException {
        return Result.suc(fileService.get(fileSearchBo));
    }

    @GetMapping("/get/{id}")
    public Result<FileVo> get(@PathVariable Integer id) throws InstantiationException, IllegalAccessException {
        return Result.suc(fileService.get(id));
    }

    @Override
    public Result<FileVo> edit(@RequestBody FileBo fileBo) throws InstantiationException, IllegalAccessException {
        return Result.suc(fileService.edit(fileBo));
    }

    @Override
    public Result<List<FileVo>> editAll(@RequestBody List<FileBo> fileBos) {
        return Result.suc(fileService.editAll(fileBos));
    }

    @Override
    public Result<FileVo> deleted(@RequestBody FileSearchBo fileSearchBo) throws InstantiationException, IllegalAccessException {
        return Result.suc(fileService.deleted(fileSearchBo));
    }

    @Override
    public Result<List<FileVo>> deletedAll(@RequestBody List<FileSearchBo> fileSearchBos) throws InstantiationException, IllegalAccessException {
        return Result.suc(fileService.deletedAll(fileSearchBos));
    }

    @Override
    public List<ImportVerifierVo> importTemplateVerifiers() {
        return null;
    }

    @Override
    public void importTemplateDownload(@RequestBody HttpServletResponse response) throws IOException {
    }

    @Override
    public Result<List<SelectVo<String, Integer, FileVo>>> option() {
        return null;
    }

    @PostMapping("/upload")
    public Result<FileVo> upload(@RequestPart("file") MultipartFile file,@RequestPart("fileBo") FileBo fileBo) throws IOException, InstantiationException, IllegalAccessException {
        fileBo.setFile(file);
        return Result.suc(fileService.upload(fileBo));
    }

    @PostMapping("/download")
    public ResponseEntity<InputStreamResource> download(@RequestBody FileSearchBo fileSearchBo) throws IOException, InstantiationException, IllegalAccessException {
        return fileService.download(fileSearchBo);
    }

    @GetMapping("/download/{id}")
    public ResponseEntity<InputStreamResource> download(@PathVariable Integer id) throws IOException, InstantiationException, IllegalAccessException {
        return fileService.download(id);
    }
}
